package com.olek;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileSystemView;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import com.olek.Model.ModelFragment;

public class MainFrame extends JFrame {
	
	private SidePanel leftPanel, rightPanel;
	private Controller controller;
	
	public MainFrame(Controller controller) {
		this.controller = controller;
		this.setTitle("Total Commander");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createMenu();
		createContent();
		
	}
	
	public SidePanel getPanel(boolean isLeft) {
		if(isLeft) {
			return leftPanel;
		} else {
			return rightPanel;
		}
	}

	private void createContent() {
		leftPanel = new SidePanel(true);
		rightPanel = new SidePanel(false);
		
		setLayout(new BorderLayout());
		JSplitPane jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
		
		jSplitPane.setDividerLocation(380);
		
		add(jSplitPane, BorderLayout.CENTER);		
		
	}

	private void createMenu() {
		
		
	}
	
	class SidePanel extends JPanel {
		
		private JComboBox<File> discBox;
		private JList<String> list;
		private DefaultListModel<String> model;
		private JTable table;
		private DefaultTableModel tableModel;
		private boolean isLeft;
		private ActionListener discBoxAction;
		private int selectedIndex = 0;
		
		public SidePanel(final boolean isLeft) {
			this.isLeft = isLeft;
			setLayout(new BorderLayout());
			discBox = new JComboBox<File>();
			
			
			add(discBox, BorderLayout.NORTH);
	
			/*
			discBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent event) {
					JComboBox<File> box = (JComboBox<File>)event.getSource();
					File file = (File)box.getSelectedItem();
					controller.updateContent(file, isLeft);
				}
			});
			*/
			discBoxAction = new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent event) {
					JComboBox<File> box = (JComboBox<File>)event.getSource();
					File file = (File)box.getSelectedItem();
					selectedIndex = box.getSelectedIndex();
					controller.updateContent(file, isLeft);
				}
			};
			discBox.addActionListener(discBoxAction);
			
			System.out.println("SidePanel construdtor after addActionListener");
			
			model = new DefaultListModel<String>();
			tableModel = new DefaultTableModel(0, 2);
			
			/*
			list = new JList<String>(model);
			JScrollPane scrollPane = new JScrollPane(list);
			*/
			
			
			table = new JTable(tableModel)
			{
	            //  Returning the Class of each column will allow different
	            //  renderers to be used based on Class
	            public Class getColumnClass(int column)
	            {
	                return getValueAt(0, column).getClass();
	            }
	            
	            public boolean isCellEditable(int row, int column)
	            {
	              return false;//This causes all cells to be not editable
	            }
	        };
	        table.setTableHeader(null);
	        table.setShowGrid(false);

	        table.getColumnModel().getColumn(0).setMinWidth(15);
	        table.getColumnModel().getColumn(0).setMaxWidth(40);
	        table.getColumnModel().getColumn(0).setPreferredWidth(20);

	        table.setRowHeight(20);
	        
			JScrollPane scrollPane = new JScrollPane(table);
			add(scrollPane, BorderLayout.CENTER);
			
		}
		
		
		public void refresh(ModelFragment fragment) {
			discBox.removeActionListener(discBoxAction);
			discBox.removeAllItems();

			for(File file : fragment.getPathRoot()) {
				discBox.addItem(file);
			}
			
			discBox.setSelectedIndex(selectedIndex);
			
			/*
			model.clear();
			for(File file : fragment.getContent()) {
				if(file.isDirectory()) {
					model.addElement("Catalog: " + file.getName());
				} else {
					model.addElement(file.getName());
				}
				
			}
			*/
			
			if (tableModel.getRowCount() > 0) {
			    for (int i = tableModel.getRowCount() - 1; i > -1; i--) {
			        tableModel.removeRow(i);
			    }
			}
			
			for(File file : fragment.getContent()) {
					tableModel.addRow(new Object[]{FileSystemView.getFileSystemView().getSystemIcon(file), file.getName()});
					model.addElement("Catalog: " + file.getName());
			}			

			discBox.addActionListener(discBoxAction);
		}
	}

}
